//
//  MKCustomAsSegmentVIew.m
//  YiQiWang
//
//  Created by moyekong on 15/3/13.
//  Copyright (c) 2015年 Apple. All rights reserved.
//

#import "MKCustomAsSegmentView.h"


@interface MKCustomAsSegmentView ()


@end

@implementation MKCustomAsSegmentView
// 设备宽、高
#define deviceWidth ([UIScreen mainScreen].bounds.size.width)
#define deviceHeight ([UIScreen mainScreen].bounds.size.height)


- (instancetype)initWithFrame:(CGRect)frame andSegmentDataArray:(NSMutableArray *)nameArray
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        [self _setupSubviewWithFrame:frame andDataArray:nameArray];
    }
    return self;
}

- (void)_setupSubviewWithFrame:(CGRect)frame andDataArray:(NSMutableArray *)dataArray {
    NSInteger dataCount = dataArray.count;
    if (dataCount != 0) {
        CGFloat averageWidth = deviceWidth / dataCount;
        CGFloat _selectedViewH = 2.0;
        CGFloat _selectedViewW = averageWidth - 20.0;
        _selectedView = [[UIView alloc] initWithFrame:CGRectMake(10.0, self.bounds.size.height - _selectedViewH, _selectedViewW, _selectedViewH)];
        _selectedView.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:91.0/255.0 blue:59.0/255.0 alpha:1.0];
        [self addSubview:_selectedView];
        
        for (int i = 0; i < dataCount; i++) {
            NSString * titleString = [dataArray objectAtIndex:i];
            CGRect tempRect = CGRectMake(i * averageWidth, 0.0, averageWidth, self.bounds.size.height);
            NSInteger btnTag = 210210210210 + i;
            [self _setupOneButtonWithTitle:titleString andFrame:tempRect andBtnTag:btnTag];
        }
    }
}

- (void)_setupOneButtonWithTitle:(NSString *)titleString andFrame:(CGRect)tempRect andBtnTag:(NSInteger)btnTag {
    UIButton * tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
    tempButton.frame = tempRect;
    [tempButton setTitle:titleString forState:UIControlStateNormal];
    if (btnTag == 210210210210) {
        [tempButton setTitleColor:[UIColor colorWithRed:255.0/255.0 green:130.0/255.0 blue:0.0/255.0 alpha:1.0] forState:UIControlStateNormal];
    } else {
        [tempButton setTitleColor:[UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0] forState:UIControlStateNormal];
    }
//    tempButton.font = [UIFont boldSystemFontOfSize:14.0];
    tempButton.titleLabel.font = [UIFont boldSystemFontOfSize:14.0];
    tempButton.tag = btnTag;
    [tempButton addTarget:self action:@selector(handleTapButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:tempButton];
}

- (void)animateSelectedViewWithNum:(NSInteger)btnNum
{
    long btnTag = btnNum + 210210210210;
    CGFloat targetBtnOriginX = 0.0;
    for (id object in self.subviews) {
        if ([object isKindOfClass:[UIButton class]]) {
            UIButton * targetBtn = (UIButton *)object;
            if (targetBtn.tag == btnTag) {
                [targetBtn setTitleColor:[UIColor colorWithRed:255.0/255.0 green:130.0/255.0 blue:0.0/255.0 alpha:1.0] forState:UIControlStateNormal];
                targetBtnOriginX = targetBtn.frame.origin.x;
            } else {
                [targetBtn setTitleColor:[UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0] forState:UIControlStateNormal];
            }
        }
    }
    CGRect originFrame = self.selectedView.frame;
    originFrame.origin.x = targetBtnOriginX + 10.0;
    [UIView animateWithDuration:0.4 animations:^{
        self.selectedView.frame = originFrame;
    }];
}

- (void)handleTapButton:(UIButton *)sender {
    long btnNum = sender.tag - 210210210210;
    if (self.delete && [self.delete respondsToSelector:@selector(clickedCustomSegmentViewButton:)]) {
        [self.delete clickedCustomSegmentViewButton:btnNum];
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
